import { request } from '../util/internals/dom_request';
import { parseSVGDocument, createEmptyResponse } from './parseSVGDocument';

export function loadSVGFromURL( url, reviver, options = {}) {
  // need to handle error properly
  return new Promise<Document>((resolve, reject) => {
    const onComplete = (r) => {
      const xml = r.responseXML;
      if (xml) {
        resolve(xml);
      }
      reject();
    };

    request(url.replace(/^\n\s*/, '').trim(), {
      onComplete,
      signal: options.signal,
    });
  })
    .then((parsedDoc) => parseSVGDocument(parsedDoc, reviver, options))
    .catch(() => {
      // this is an unhappy path, we dont care about speed
      return createEmptyResponse();
    });
}
